#include <QtGlobal>
#include <QtCore/QFile>
#include "qconstants.h"
#include "qzodiac.h"
#include "qhtmlparser.h"


QHtmlParser::QHtmlParser(const QString &strPath) : m_strPath(strPath)
{
    this->init();  // Q_ASSERT may cause error in release mode using vs2005 ?!
}

bool QHtmlParser::init()
{
    QFile file(m_strPath);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        return false;
    }

    QString data = QString::fromUtf8(file.readAll());
    int a = data.indexOf("lotstars");
    int b = data.indexOf("lotconts");
    int starsBegin = data.lastIndexOf("<div", a);
    int starsEnd = data.lastIndexOf("<div", b);
    int contsBegin = starsEnd;
    int contsEnd = data.indexOf("<div", contsBegin+1);
    m_strStars = data.mid(starsBegin, starsEnd-starsBegin);
    m_strContents = data.mid(contsBegin, contsEnd-contsBegin);
    file.close();

    return true;
}

QString QHtmlParser::getAllFormatted(const QDate &dtDate, const QZodiac &zodiac) const
{
    QConstants constants;
    QString str1 = constants.value("ast.title.all");
    QString str2 = constants.value("ast.title.love");
    QString str3 = constants.value("ast.title.work");
    QString str4 = constants.value("ast.title.money");
    QString str5 = constants.value("ast.title.health");
    QString str6 = constants.value("ast.title.talk");
    QString str7 = constants.value("ast.title.luckcolor");
    QString str8 = constants.value("ast.title.lucknumber");
    QString str9 = constants.value("ast.title.zodiac");

    QString strDate = dtDate.toString("yyyy-MM-dd");
    QString allContents = "";

#ifndef Q_OS_SYMBIAN
    allContents.append(strDate).append(" ").append(zodiac.toString()).append(QChar::LineSeparator).append(QChar::LineSeparator);
#endif
    allContents.append(zodiac.toString()).append(QChar::LineSeparator).append(QChar::LineSeparator);
    // all
    allContents.append(this->getStarFormatted(str1)).append(QChar::LineSeparator);
    // love
    allContents.append(this->getStarFormatted(str2)).append(QChar::LineSeparator);
    // work
    allContents.append(this->getStarFormatted(str3)).append(QChar::LineSeparator);
    // money
    allContents.append(this->getStarFormatted(str4)).append(QChar::LineSeparator);
    // health
    allContents.append(this->getContentFormatted(str5)).append(QChar::LineSeparator);
    // talk
    allContents.append(this->getContentFormatted(str6)).append(QChar::LineSeparator);
    // luck color
    allContents.append(this->getContentFormatted(str7)).append(QChar::LineSeparator);
    // luck number
    allContents.append(this->getContentFormatted(str8)).append(QChar::LineSeparator);
    // zodiac
    allContents.append(this->getContentFormatted(str9)).append(QChar::LineSeparator).append(QChar::LineSeparator);
    // major content
    allContents.append(this->getMajorContentFormatted()).append(QChar::LineSeparator).append(QChar::LineSeparator);

    return allContents;
}

int QHtmlParser::getStar(const QString &strKey) const
{
    int i = m_strStars.indexOf(strKey);
    if (i != -1) {
        int begin = m_strStars.lastIndexOf("<div", i);
        int end = m_strStars.indexOf("<div", i);
        QString strSection = m_strStars.mid(begin, end-begin);
        return strSection.count(QString("star.gif"));
    }

    return -1;
}

QString QHtmlParser::getStarFormatted(const QString &strKey) const
{
    QString strCtrl = QString();
#ifdef Q_OS_WIN
    strCtrl = QString(QConstants::Esc);
#else
    strCtrl = QString(QConstants::Esc).append(QConstants::Esc);
#endif

    QString strBuffer = QString();
    strBuffer.append(QConstants::Round).append(strKey).append(QConstants::Colon);
    QString strStars = QString();
    int star = this->getStar(strKey);
    for (int i = 0; i < star; i++) {
        strStars.append(QConstants::Star);
    }

#ifdef Q_OS_SYMBIAN
    strBuffer.append(strStars);
#else
    if (star == 5) {
        strBuffer.append(strCtrl).append("[1;31m");
    }
    strBuffer.append(strStars);
    if (star == 5) {
        strBuffer.append(strCtrl).append("[m");
    }
#endif

    return strBuffer;
}

QString QHtmlParser::getContent(const QString &strKey) const
{
    int i = m_strStars.indexOf(strKey);
    if (i != -1) {
        int begin = m_strStars.indexOf("<p>", i) + 3;
        int end = m_strStars.indexOf("</p>", i);
        QString strSection = m_strStars.mid(begin, end-begin);
        return strSection.trimmed();
    }

    return QString();
}

QString QHtmlParser::getContentFormatted(const QString &strKey) const
{
    QString strBuffer = QString();
    strBuffer.append(QConstants::Round).append(strKey).append(QConstants::Colon);

    QString content = this->getContent(strKey);
    strBuffer.append(content);

    return strBuffer;
}

QString QHtmlParser::getMajorContent() const
{
    int firstBegin = m_strContents.indexOf(">") + 1;
    int firstEnd = m_strContents.indexOf("<", firstBegin);
    int secondBegin = m_strContents.indexOf(">", firstBegin) + 1;
    int secondEnd = m_strContents.lastIndexOf("<");
    QString first = m_strContents.mid(firstBegin, firstEnd-firstBegin).trimmed();
    QString second = m_strContents.mid(secondBegin, secondEnd-secondBegin).trimmed();

    return QString("%4%4%1%3%4%4%2").arg(first, second, QString(QChar::LineSeparator), QString(QConstants::Space));
}

QString QHtmlParser::getMajorContentFormatted() const
{
    return this->getMajorContent();
}
